/**
 * Created by zzw on 2016/5/20.
 * 申请提现
 */

$(function(){
    showDefualtBankCard();
    agentApplicationMoney();
});

//显示默认银行卡
function showDefualtBankCard(){
    function getUrlPage(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }
    var id = getUrlPage('id');
    var param = JSON.stringify({"token": localStorage.token});
    $.fn.serviceInvoke(serviceAddress.Address, 'agent_app_AgentWithdraw','agentWithdrawList.action', param, function (result) {
       if(result.msg=="success"){
           var record = '';
           if(result.list.length==0){
               $(".container").html("没有记录");
           }else{
               for(var i=0;i<result.list.length;i++){
                   record+='<div class="media"><div class="media-left media-top" style="vertical-align: middle;width: 400px;"><h5>'+result.list[i].ddmmTime+'</h5><h6>'+result.list[i].datetime+'</h6></div><div class="media-body" style="border-bottom: 1px solid rgb(247,247,247)"><p style="margin-top: 15px;font-size: 15px;">'+result.list[i].mark+'</p></div></div>';
               }
               $(".container").html(record);
           }
           if(result.entity.bankName==""){
               $(".bankNo").html("<h5></h5></span><span>请选择银行卡</span>");
           }else{
               var img = result.entity.bimg;
               var src = "../images/"+img+".png";
               $(".bankImg img").attr("src",src);
               $(".bankNo").html("<div>"+ result.entity.bankName+"</div><div>储蓄卡 | "+result.entity.bankNo+"</div>");
           }
           $(".cash").attr("placeholder","最高可提现"+result.entity.totalMoney+"元");
       }else{
           $(".bankNo").html("<h5></h5><span>请选择银行卡</span>");
           $(".container").html("没有记录");
       }

    })
}

//申请提现方法
function agentApplicationMoney(){
    $(".btn").bind("click",function(){
        if($(".bankNo").text()=="请选择银行卡"){
            alert("未选择银行卡，请先绑定银行阿卡！");
            return false;
        }
        var applicateMoney = $(".cash").val();
        if(isNaN(applicateMoney)){
            alert("输入的金额无效");
            return false;
        }
        if(applicateMoney<500){
             alert("提现金额必须大于500元");
             return false;
        }
        debugger;
        if((applicateMoney%100)!=0){
            alert("提现金额必须为100的整数倍");
            return false;
        }

        if(parseInt(applicateMoney)>parseInt($(".cash").attr("placeholder").substring(5,$(".cash").attr("placeholder").length-1))){
            alert("提现金额不能大于最高提现金额，请重新输入。");
            $(".cash").val("");
            return false;
        }
        var param = JSON.stringify({"applicateMoney":applicateMoney,"token": localStorage.token});
        $.fn.serviceInvoke(serviceAddress.Address, 'agent_app_AgentWithdraw','agentApplicationMoney.action', param, function (result) {
            debugger;
            if(result.msg=="success"){
                alert("提现成功！");
                location.href="advanceCash.html";
            }else{
                alert("可提现余额不足！");
            }
        })
    })
}
